var searchCity
var date
var latitude = ''
var lontitude = ''
const app = getApp()
var isLoadMore = false
var isRefresh = false
var cityName = ''
var areaCode = ''
const toast = require('../../utils/toast.js')
Page({
  /**
   * 页面的初始数据 
   */
  data: {
    cityName: "北京",
    searchColor: "#999999",
    searchText: "请输入城市或地理坐标",
    dateType: "入住日期",
    dateImage: "../../images/arraw_little_bottom.png",
    locationType: "地理位置",
    locationImage: "../../images/arraw_little_bottom.png",
    roomType: "房屋类型",
    roomImage: "../../images/arraw_little_bottom.png",
    recommendType: "推荐类型",
    recommendImage: "../../images/arraw_li  ttle_gray_bottom.png",
    list: null,
    messageCode: "",
    hideBottom: true,
    loadMoreData: "加载中...",
    is_bottom: false,
    toView: 20,
    allPages: 1,
    bingPage: 1,
    refreshTime: '',
    hideRefresh: true,
    startTime: '',
    endTime: '',
    isGetData: false,
    iss: false,
    firstIndex: 0,
    secondIndex: 0,
    thirdIndex: 0,
    roomState: 0
  },
  toast: toast,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getIntentData(options);
    console.log(options);
    var date = new Date();
    this.setData({
      refreshTime: date.toLocaleTimeString(),
      isGetData: false
    })
    this.connectNetForData();
    this.connectNetForLocation();
  },


  /**
   * 请求网络获取数据 
   */
  connectNetForData() {
    var that = this;
    var currentPage = that.data.bingPage;
    var dd = app.baseNet + 'roomInfo/queryRoomInfoDtos?latitude=' + latitude + '&longitude=' + lontitude + '&areaCode=' + areaCode +
      +'&startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&bingPage=' + currentPage;
    console.log(dd)
    if (!this.data.isGetData) {
      // 初始获取数据
      wx.showLoading({
        title: '拼命加载中...'
      })
    }
    wx.request({
      url: app.baseNet + 'roomInfo/queryRoomInfoDtos',
      data: {
        latitude: latitude,
        longitude: lontitude,
        areaCode: areaCode,
        startTime: that.data.startTime,
        endTime: that.data.endTime,
        bingPage: currentPage
      },
      method: 'POST',
      header: {
        // 'Content-Type': 'application/json'
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res.statusCode)
        if (res.statusCode == 200) {
          var roomList = res.data.roomDtos;
          console.log(roomList)
          if (isLoadMore) { //加载更多
            isLoadMore = false;
            var data = that.data.list.concat(roomList)
            that.setData({
              hideBottom: true,
              loadMoreData: "加载中...",
              list: data
            })
          }

          if (!that.data.isGetData) { //初始获取数据
            wx.hideLoading(roomList + " :: " + currentPage)
            that.setData({
              messageCode: res.data.messageCode,
              list: roomList,
              allPages: res.data.pags,
              isGetData: true
            })
          }

          if (isRefresh) { //刷新数据
            isRefresh = false;
            console.log(roomList + " :: " + currentPage)
            that.setData({
              messageCode: res.data.messageCode,
              list: roomList,
              allPages: res.data.pags,
              hideRefresh: true,
            })
          }
        } else {
          toast.showSystemAbnormalToast()
        }
      }
    })
  },
  connectNetForLocation() {
    var that = this
    console.log(' location_url :  ' + app.baseNet + 'tradearea/queryTradeareaOne?cityName=' + that.data.cityName)
    wx.request({
      url: app.baseNet + 'tradearea/queryTradeareaOne',
      data: {
        cityName: that.data.cityName,
      },
      method: 'POST',
      header: {
        // 'Content-Type': 'application/json'
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.statusCode == 200) {
          var data = res.data
          if (data.messcode == '001') {
            that.setData({
              tradeareaOneList: data.tradeareaOneList,
              tradeareaTwoList: data.tradeareaTwoList,
              tradeareaThreeList: data.tradeareaThreeList
            })
          }
        }
      } 
    })
  },

  getIntentData(options) {
    searchCity = options.destination;
    console.log(searchCity);
    date = options.checkDate;
    //正式上线的
    latitude = options.latitude;
    lontitude = options.longitude;

    areaCode = options.areaCode;
    if (areaCode == null || typeof areaCode == 'undefined') {
      areaCode = ''
    }
    cityName = options.cityName;

    if (cityName != '' && typeof cityName != 'undefined') {
      this.setData({
        cityName: cityName
      })
    }
    // //page = 3 
    // latitude = 31.157387;
    // lontitude = 121.81502

    //  空数据
    // latitude = "38.842026";
    // lontitude = "105.68902";
    if (typeof searchCity != 'undefined' || searchCity) {
      this.setData({
        searchText: searchCity,
        searchColor: "#333"
      })
    } else {
      this.setData({
        searchColor: "#999999"
      })
    }

    this.data.startTime = options.startTime
    this.data.endTime = options.endTime

    if (this.data.startTime != '' && this.data.endTime != '') {
      var mm = this.data.startTime.split('-');
      var dd = this.data.endTime.split('-')
      this.setData({
        dateType: mm[1] + '.' + mm[2] + '-' + dd[1] + '.' + dd[2]
      })
    }

  },
  click: function(e) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.iss) {
      // 初始获取数据
      wx.showLoading({
        title: '拼命加载中...'
      })
      this.setData({
        iss: false
      })
    }
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },
  refreshData: function() {
    console.log("refresh下拉刷新");
    isRefresh = true;
    isLoadMore = false
    this.setData({
      hideRefresh: false,
      hideBottom: true,
      isGetData: true,
      bingPage: 1
    })
    this.connectNetForData();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  loadMore: function() {
    var that = this;
    isLoadMore = true;
    isRefresh = false;
    if (isLoadMore) {
      console.log("loadmore上拉加载更多");
      //当前页是最后一页
      if (that.data.bingPage == that.data.allPages) {
        isLoadMore = false;
        that.setData({ //ds
          hideBottom: false,
          loadMoreData: "已经加载完毕",
          isGetData: true,
          is_bottom: true
        })
        return
      } else {
        var currentPage = that.data.bingPage;
        currentPage = currentPage + 1;
        setTimeout(function() {
          that.setData({
            hideBottom: false,
            loadMoreData: "加载中...",
            is_bottom: false,
            isGetData: true,
            bingPage: currentPage
          })
          that.connectNetForData();
        }, 100)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 日期选择
   */
  dataSlected: function(e) {
    wx.navigateTo({
      url: '../calendar/calendar?isRoomList=' + true + '&startTime=' + this.data.startTime + '&endTime=' + this.data.endTime,
    })
  },
  /**
   * 地理位置选择 
   */
  locationSelected: function(e) {
    console.log('tradeareaOneList : ' + this.data.tradeareaOneList)
    var one = JSON.stringify(this.data.tradeareaOneList)
    var two = JSON.stringify(this.data.tradeareaTwoList)
    var three = JSON.stringify(this.data.tradeareaThreeList)
    console.log('one : ' + one)
    wx.navigateTo({
      url: '../location/location?tradeareaOneList=' + one + '&tradeareaTwoList=' + two + '&tradeareaThreeList=' + three + '&firstIndex=' + this.data.firstIndex + '&secondIndex=' + this.data.secondIndex + '&thirdIndex=' + this.data.thirdIndex,
    })
  },

  /**
   * 房屋类型选择
   */
  roomTypeSelected: function(e) {
    wx.navigateTo({
      url: '../roomtype/roomtype?roomState=' + this.data.roomState,
    })
  }
})